﻿@{
    ViewBag.Title = "原料入库";
    var proList = ViewBag.ProList as List<CubicFWNet.Data.Models.Product>;
    var formateList = ViewBag.FormateList as List<CubicFWNet.Data.Models.MaterialFormat>;
    var product = ViewBag.Product as CubicFWNet.Data.Models.Product;
}

<section class="content-header">
    <h1>
        原料入库
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active">原料入库</li>
    </ol>
</section>

<section class="content">
    <form class="form-horizontal" method="post" id="formMain">
        <div class="box box-solid">
            <div class="box-body">

                <div class="row">
                    <input type="hidden" id="PID" name="PID" value="@product.Id" />

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProductName" class="col-sm-3 control-label">选择原料</label>
                        <div class="col-sm-9">
                            <span onclick="ShowProList()" class="btn btn-primary ">选择原料</span>
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProName" class="col-sm-3 control-label">原料信息</label>
                        <div class="col-sm-9">
                            <span onclick="ShowModal('@product.Id')" name="ProName" id="ProName" class="form-control linkLayout">@product.Name</span>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <input type="hidden" id="OID" name="OID" />
                    <input type="hidden" id="UID" name="UID" />

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProductName" class="col-sm-3 control-label">选择规格</label>
                        <div class="col-sm-9">
                            <select class="form-control" id="selFormate" name="Formate"> 
                                <option value="">请选择原料规格</option>                             
                                @if (null != formateList)
                                {
                                    foreach (var item in formateList)
                                    {
                                        <option value="@item.Id">@item.Format</option>
                                    }
                                }

                            </select>
                        </div>
                    </div>

                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Amount" class="col-sm-3 control-label">数量</label>
                        <div class="col-sm-9">
                            <input type="number" class="form-control" id="Amount" name="Amount" maxlength="20" min="0" />
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Memo" class="col-sm-3 control-label">备注</label>
                        <div class="col-sm-9">
                            <textarea rows="3" class="form-control" id="Memo" name="Memo" maxlength="500"></textarea>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Creator" class="col-sm-3 control-label">创建人</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Creator" name="Tolerance" value="@ViewBag.RealName" readonly="readonly" />
                        </div>
                    </div>
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="CreateTime" class="col-sm-3 control-label">创建时间</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CreateTime" value="@DateTime.Now.ToString("yyyy-MM-dd HH:mm")" readonly="readonly" />
                        </div>
                    </div>
                </div>
            </div>

            <div class="box-footer">
                <span class="btn btn-primary " onclick="FormSub()">确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;
            </div>

        </div>
    </form>
</section>

<!--选择原料-->
<div class="modal" id="proList" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">选择原料</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="form-group  col-sm-12">
                        <label for="PName" class="col-sm-3 control-label" onclick="GetProduct()"><span style="color:dodgerblue">搜索原料</span></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="PName" name="PName" placeholder="请输入原料名称..." onkeyup="GetProduct()" />
                        </div>
                    </div>
                </div>
                <div id="cusProList">
                    @if (proList != null)
                    {
                        foreach (var item in proList)
                        {
                            <span class="btn btn-default productItem" data-proID="@item.Id" data-sel="0" style="margin:8px">@item.Name</span>
                        }
                    }
                </div>
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        //提交表单
        function FormSub() {
            var prpName = $("#ProName").html().trim();
            if (prpName == "" || prpName == null) {
                $.modalAlert("请选择原料", "warning");
                return false;
            }

            var formate = $("#selFormate").val().trim();
            if (formate == "" || formate == null) {
                $.modalAlert("请选择原料规格！", "warning");
                return false;
            }

            var amount = $("#Amount").val().trim();
            if (amount == "" || amount == null) {
                $.modalAlert("数量有误！", "warning");
                return false;
            }
            if (amount < 0) {
                $.modalAlert("数量有误！", "warning");
                return false;
            }

            $("#formMain").submit();
        }

        //选择原料弹窗
        function ShowProList() {
            $("#proList").modal();
        }

        //弹窗显示原料信息
        function ShowModal(id) {
            var options = {
                id: null,
                title: '原料信息',
                width: "90%",
                height: "90%",
                url: "../Product/Info?PID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }

        //选择原料事件
        $(".productItem").click(function () {
            var id = $(this).attr("data-proID");
            location.href = "PutInMaterials?PID=" + id;
        });

        //搜索原料
        function GetProduct() {
            var name = $("#PName").val().trim();

            var proList = $("#cusProList .productItem");
            var num = proList.size();

            for (var i = 0; i < num; i++) {
                var inName = $("#cusProList .productItem").eq(i).html();
                if (inName.indexOf(name) >= 0) {
                    $("#cusProList .productItem").eq(i).show();
                }
                else {
                    $("#cusProList .productItem").eq(i).hide();
                }
            }
        }
    </script>
}